"""iClouds URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.contrib.staticfiles.views import serve

from django.conf import settings
from django.conf.urls.static import static
from .views import *
from .allRoute import get_all_url_dict

from rest_framework.compat import coreapi
from rest_framework.schemas.coreapi import SchemaGenerator, distribute_links

DESCRIPTION = """
        所有接口文档。包括以下应用：
        1 api: 收单接口.
        2 webapi: 后台接口.


"""


urlpatterns = [
    path("", hello.as_view()),
    path("favicon.ico/", serve, {"path": "images/favicon.ico"}),
    path(
        "api/",
        include(("mysqldb.urls", "mysqldb"), namespace="mysqldb"),
        name="公用接口",
    ),
    path(
        "user/",
        include(("UserManager.urls", "UserManager"), namespace="UserManager"),
        name="用户管理器",
    ),
    path(
        "place/",
        include(("PlaceManager.urls", "PlaceManager"), namespace="PlaceManager"),
        name="场所管理器",
    ),
    path(
        "loan/",
        include(("allinpay.urls", "allinpay"), namespace="payapi"),
        name="收单管理器",
    ),
    path(
        "lizhu/",
        include(("LizhuManager.urls", "LizhuManager"), namespace="LizhuManager"),
        name="利住管理器",
    ),
    path(
        "vip/",
        include(("VipManager.urls", "VipManager"), namespace="VipManager"),
        name="会员管理器",
    ),
    path(
        "call/",
        include(("CallManager.urls", "CallManager"), namespace="CallManager"),
        name="呼叫管理器",
    ),
    path(
        "report/",
        include(("ReportManager.urls", "ReportManager"), namespace="ReportManager"),
        name="报表管理器",
    ),
    path(
        "staff/",
        include(("StaffManager.urls", "StaffManager"), namespace="StaffManager"),
        name="员工管理器",
    ),
    path(
        "sync/",
        include(("SyncManager.urls", "SyncManager"), namespace="SyncManager"),
        name="同步管理器",
    ),
    path(
        "hard/",
        include(("AssetsManager.urls", "AssetsManager"), namespace="AssetsManager"),
        name="资产管理器",
    ),
    path(
        "wechat/",
        include(("WeChatManager.urls", "WeChatManager"), namespace="WeChatManager"),
        name="微信小程序管理器",
    ),
    path(
        "wepay/",
        include(("WePayManager.urls", "WePayManager"), namespace="WePayManager"),
        name="微信支付管理器",
    ),
    path(
        "fsyg/",
        include(("fsygManager.urls", "fsygManager"), namespace="fsygManager"),
        name="凡商管理器",
    ),
    path(
        "bars/",
        include(("BarsManager.urls", "BarsManager"), namespace="BarsManager"),
        name="员工直连管理器",
    ),
    path(
        "show/",
        include(("MoniterManager.urls", "MoniterManager"), namespace="MoniterManager"),
        name="大屏机位图管理器",
    ),
    path(
        "game/",
        include(("GameManager.urls", "GameManager"), namespace="GameManager"),
        name="游戏活动配置管理器",
    ),
    path(
        "cqbar/",
        include(("CQBarManager.urls", "CQBarManager"), namespace="CQBarManager"),
        name="重庆会员管理器",
    ),
    path(
        "web/",
        include(
            ("WebActionManager.urls", "WebActionManager"), namespace="WebActionManager"
        ),
        name="页面管理器",
    ),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


class CustomSchemaGenerator(SchemaGenerator):
    keys = get_all_url_dict(urlpatterns)

    def get_schema(self, request=None, public=False):
        self._initialise_endpoints()
        links = self.get_links(None if public else request)
        if not links:
            return None

        url = self.url
        if not url and request is not None:
            url = request.build_absolute_uri()

        distribute_links(links)
        links = dict(links)
        sess = links.copy()
        for k, v in sess.items():
            v = dict(v)
            for ck, cv in v.items():
                if self.keys.get(f"/{k}/{ck}/"):
                    links[k].update({self.keys.get(f"/{k}/{ck}/"): links[k].pop(ck)})
                else:
                    links[k].update({ck: links[k].pop(ck)})
        # print(links)
        return coreapi.Document(
            title=self.title, description=self.description, url=url, content=links
        )


if settings.DOCS:
    from rest_framework.documentation import include_docs_urls

    schema_view = include_docs_urls(
        title="iClouds", description=DESCRIPTION, generator_class=CustomSchemaGenerator
    )
    urlpatterns.append(
        # 文档页面
        path("docs/", schema_view, name="接口文档"),
    )
